﻿# Create by HR
# time 3/20/2017
# coding=utf-8




import sys
import xlwt
import os
import pyExcelerator
import warnings

default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
    reload(sys)
    sys.setdefaultencoding(default_encoding)

class Load_txt:
    # data_list = []
    # path = ""

    def __init__(self , _path):
        self.path = _path
        self.x_left = 0
        self.y_left = 0
        self.x_right = 0
        self.y_right = 0

    def Load(self):
        self.data_list = []
        txt_file = open(self.path, 'r')
        line = txt_file.readline()
        label = u""
        is_left = True
        i= 0
        while line:
            data = line.split(',')
            if len(data) >= 3:
                if data[2][0] == 'X':
                    if is_left:
                        self.x_left = data[2].split(' ')[0].split("=")[1].split(u"米")[0]
                        self.y_left = data[2].split(' ')[2].split("=")[1].split(u"米")[0]
                        if len(data) > 3:
                            if len(data[3].split("-")) == 1:
                                label = data[3]
                            else:
                                label = data[3].split("-")[1]
                            data[3] = data[3] + ",DMZ"
                        else:
                            label = label.split("\n")[0]
                            data.append(label + ",DMZ")
                        is_left = False
                    else:
                        self.x_right = data[2].split(' ')[0].split("=")[1].split(u"米")[0]
                        self.y_right = data[2].split(' ')[2].split("=")[1].split(u"米")[0]
                        if len(data) > 3:
                            if len(data[3].split("-")) == 1:
                                label = data[3]
                            else:
                                label = data[3].split("-")[1]
                            data[3] = data[3] + ",DMY"
                        else:
                            label = label.split("\n")[0]
                            data.append(label + ",DMY")
                    data.pop(2)
                elif len(data[2].split("-")) == 1:
                    label = data[2]
                else:
                    label = data[2].split("-")[1]
            elif len(data) == 2:
                data[1] = data[1].split("\n")[0]
                data.append(label)
                label = label.split(",")[0]
            self.data_list.append(data)
            line = txt_file.readline()
            i= i+1
        # if self.x_left == 0 or self.y_right == 0 or self.x_right == 0 or self.y_left == 0:
        #     warnings.warn(self.path + "基点坐标有误，请检查txt文件！")
        # return self.data_list

# loader = Load_txt("C:\Users\Huang\Documents\code\python\DM\data\DM12.txt")
# loader.Load()
# # print loader.data_list[0][2].decode("ascii").encode("utf-8")
# print unicode(loader.x_left),loader.x_right,loader.y_left,loader.y_right

class Excel_Creator:
    path = ""
    wb = xlwt.Workbook()
    style = pyExcelerator.XFStyle()
    def __init__(self , _path):
        self.path = _path
        self.HSheet = self.wb.add_sheet(u"横断面",cell_overwrite_ok=True)
        #set table's style
        al = pyExcelerator.Alignment()
        al.horz = pyExcelerator.Alignment.HORZ_CENTER
        al.vert = pyExcelerator.Alignment.VERT_CENTER
        self.style.alignment = al
        font = pyExcelerator.Font()
        font.name = "Microsoft YaHei UI"
        self.style.font = font
        borders = pyExcelerator.Borders
        borders.bottom = 2
        borders.top = 2
        borders.left = 2
        borders.right = 2
        self.style.borders = borders

    def Write_Templete(self, _start_Index, _prj_name, _section_name,_measurer, _scale, _tbl_creator, _date):
        self.HSheet.write_merge(_start_Index, _start_Index, 0, 4)
        self.HSheet.write(_start_Index,0,_prj_name + u"断面" + _section_name + u"成果表")
        self.HSheet.write(_start_Index + 2, 0, u"断面编号：")
        self.HSheet.write(_start_Index + 2, 1, _section_name)
        self.HSheet.write(_start_Index + 2, 3,  u"测量者：")
        self.HSheet.write(_start_Index + 2, 4,  _measurer)

        self.HSheet.write(_start_Index + 3, 0,  u"测量比例尺：")
        self.HSheet.write(_start_Index + 3, 1,  _scale)
        self.HSheet.write(_start_Index + 3, 3,  u"制表者：")
        self.HSheet.write(_start_Index + 3, 4,  _tbl_creator)

        self.HSheet.write(_start_Index + 4, 0,  u"测量日期：")
        self.HSheet.write_merge(_start_Index + 4, _start_Index + 4, 1, 2)
        self.HSheet.write(_start_Index + 4, 1,  _date)
        self.HSheet.write(_start_Index + 4, 3,  u"检查者：")

        self.HSheet.write(_start_Index + 5, 0, u"基点坐标：")

        self.HSheet.write(_start_Index + 6, 0, u"X左＝")
        self.HSheet.write(_start_Index + 6, 3, u"Y左＝")

        self.HSheet.write(_start_Index + 7, 0, u"X右＝")
        self.HSheet.write(_start_Index + 7, 3,  u"Y右＝")

        self.HSheet.write(_start_Index + 8, 4, u"单位：米")

        self.HSheet.write(_start_Index + 9, 0, u"序号")
        self.HSheet.write(_start_Index + 9, 1, u"间距")
        self.HSheet.write(_start_Index + 9, 2, u"累距")
        self.HSheet.write(_start_Index + 9, 3, u"高程")
        self.HSheet.write(_start_Index + 9, 4, u"备注")
        return _start_Index + 10

    def Save_Excel(self):
        print self.path
        self.wb.save(self.path + u"\\断面成果.xls")

# ex = Excel_Creator("C:\Users\Huang\Documents\code\python\DM\Txt2xlsx")
# ex.Write_Templete(5,u"麻江县龙口水库测量工程","DM1",u"冯刚"," "," "," ")
# ex.Save_Excel()







